package com.dcy.znoa.service.sys.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.sys.model.role.SelectRoleModel;
import com.dcy.znoa.dao.publiz.CompanyDao;
import com.dcy.znoa.dao.sys.OperRoleDao;
import com.dcy.znoa.dao.sys.RoleDao;
import com.dcy.znoa.dao.user.OperDao;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.sys.OperRole;
import com.dcy.znoa.domain.sys.Role;
import com.dcy.znoa.service.sys.RoleService;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;
    @Autowired
    OperRoleDao operRoleDao;
    @Autowired
    OperDao operDao;
	@Autowired
	CompanyDao companyDao;
    
    @Override
    public JsonForList selectList(SelectRoleModel model) throws InvocationTargetException, IllegalAccessException {
        List<Role> list;       
        
        EntityWrapper<Role> ew = new EntityWrapper<>();

        ew.setEntity(Role.builder().compId(model.getCompId()).build());
        ew.like("role_name",model.getRoleName());

        Page<Role> page = new Page(model.getPageNum(), model.getPageSize());
        list = roleDao.selectPage(page, ew);
        //2 分页方法2 ，需要model里面包含 RowBounds 类，查询调用分页方法,但是 RowBounds里面的参数是MySQL 对应的，有点反人类，第一页要从0开始。
        //list = companyDao.selectPage(model.getRowBounds(),ew);
        return new JsonForList(list, page.getTotal());
    }

    @Override
    public JsonCommon insert(Role role) {
		//查询：同一个公司下不允许有同名的角色
		List list = roleDao.selectList( new EntityWrapper<Role>().eq("comp_id",role.getCompId()).eq("role_name",role.getRoleName()).eq("valid_flag",1));
		if(!list.isEmpty()){
			return new JsonCommon(JsonCommon.INSERT_ERROR_CODE,"不允许有同名的角色");
		}
        Integer result = roleDao.insert(role);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @Override
    public JsonCommon updateById(Role var1) {
		//查询当前角色对应的企业ID
		Role role = roleDao.selectById(var1.getRoleId());
		if(role.getCompId()==null){
			return  new JsonCommon("","请联系管理员先为此角色绑定一个公司");
		}
		EntityWrapper ew =  new EntityWrapper<Role>();
		if(StringUtils.isNotBlank(var1.getRoleName())){
			ew.eq("role_name",var1.getRoleName());
		}
		//检查是否有角色名，查询库里同名的部门是否有不同的id
		List<Role> list = roleDao.selectList( new EntityWrapper<Role>().eq("comp_id",role.getCompId()).eq("role_name",var1.getRoleName()).eq("valid_flag",1));
		boolean flag;
		if(list.isEmpty()){ //允许修改
			flag = true;
		}else if(list.size()==1){//判断id是不是一样的
			if(var1.getRoleId().longValue() == list.get(0).getRoleId().longValue()){//允许修改
				flag = true;
			}else{
				flag = false;
			}
		}else{// >1，不允许修改
			flag = false;
		}
		if(flag == false){
			return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE,"存在同名角色，不允许修改为同名角色");
		}
		//如果角色有对应的员工，则不允许禁用
		if(null != var1.getValidFlag() && var1.getValidFlag() == 0){
			//查询有没有员工
			List<OperRole> operRoleList = operRoleDao.selectList( new EntityWrapper<OperRole>().eq("role_id",var1.getRoleId()).eq("valid_flag",1));
			if(!operRoleList.isEmpty()){
				return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE,"该角色对应有员工，不允许禁用");
			}
		}

        Integer result = roleDao.updateById(var1);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

	@Override
	public JsonForList selectUserListByRoleId(SelectRoleModel model) {
        List<OperRole> list;       
        
        EntityWrapper<OperRole> ew = new EntityWrapper<>();
        ew.eq("role_id",model.getRoleId());
        
        //获取所属角色ID
        list = operRoleDao.selectList(ew);
        
        if (!list.isEmpty()) {
        	List<Long> idList = new ArrayList<>();
        	for (OperRole operRole : list) {
        		idList.add(operRole.getOperId());
    		}
        	
        	List<Oper> operList;     
        	//查询角色实体
        	EntityWrapper<Oper> ewOper = new EntityWrapper<>();
        	ewOper.in("oper_id", idList);
        	//部门、用户手机号、用户姓名查询
        	if (model.getDepId() != null) {
        		ewOper.eq("dep_id", model.getDepId());
			}
        	if (model.getOperName() != null) {
        		ewOper.like("oper_name", model.getOperName());
			}
        	if (model.getPhoneNo() != null) {
        		ewOper.like("phone_no", model.getPhoneNo());
			}
        	
        	if (model.getValidFlag() != null) {
        		ewOper.eq("valid_flag", model.getValidFlag());
			}
        	
        	Page<Role> page = new Page(model.getPageNum(), model.getPageSize());
        	operList = operDao.selectPage(page, ewOper);
        	
        	if (operList.isEmpty()) {
        		return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
			}else {
				return new JsonForList(operList, page.getTotal());
			}
        	
		}else {
			return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
	}

	@Override
	public JsonForList selectUserListNotOfRoleId(SelectRoleModel model) {
		Role role = roleDao.selectById(model.getRoleId());
		if (role == null) {
			return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}

		List<OperRole> list;
		EntityWrapper<OperRole> ew = new EntityWrapper<>();
		ew.eq("role_id", model.getRoleId());

		// 获取所属角色ID
		list = operRoleDao.selectList(ew);

		List<Long> idList = new ArrayList<>();
		for (OperRole operRole : list) {
			idList.add(operRole.getOperId());
		}

		List<Oper> operList;
		EntityWrapper<Oper> ewOper = new EntityWrapper<>();
		ewOper.eq("comp_id", role.getCompId());
		if (!list.isEmpty()) {
			ewOper.notIn("oper_id", idList);
		}
		// 部门、用户手机号、用户姓名查询
		if (model.getDepId() != null) {
			ewOper.eq("dep_id", model.getDepId());
		}
		if (model.getOperName() != null) {
			ewOper.like("oper_name", model.getOperName());
		}
		if (model.getPhoneNo() != null) {
			ewOper.like("phone_no", model.getPhoneNo());
		}

		if (model.getValidFlag() != null) {
			ewOper.eq("valid_flag", model.getValidFlag());
		}
		Page<Role> page = new Page(model.getPageNum(), model.getPageSize());
		operList = operDao.selectPage(page, ewOper);

		if (operList.isEmpty()) {
			return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		} else {
			return new JsonForList(operList, page.getTotal());
		}

	}


}
